#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void insertionSort(int V[], int tam);
void receber_vetor(int V[]);
void enviar_vetor(int V[],int porta,char* host);
void error(const char *msg)
{
    perror(msg);
    exit(0);
}

void main(int argc, char *argv[])
{
    int vetor[500],vetor1[250],vetor2[250],tam=500,cont=0,i;
    FILE *fp;

    fp = fopen ("node15.txt", "w");
    
    receber_vetor(vetor1);
    fprintf(fp,"Primeiro Vetor\n[ ");
    i=0;
    while(i<250){
        fprintf(fp,"%d, ",vetor1[i]);
        i++;
    }
    fprintf(fp,"]\n\n");

    receber_vetor(vetor2);
    fprintf(fp,"Segundo Vetor\n[ ");
    i=0;
    while(i<250){
        fprintf(fp,"%d, ",vetor2[i]);
        i++;
    }
    fprintf(fp,"]\n\n");

    for (i = 0; i < 250; i++){
        vetor[cont] = vetor1[i];
        vetor[cont+250] = vetor2[i];
        cont++;
    }

    insertionSort(vetor,tam);
    fprintf(fp,"Vetor Ordenado \n[ ");
    i=0;
    while(i<tam){
        fprintf(fp,"%d, ",vetor[i]);
        i++;
    }
    fprintf(fp,"]\n\n");

    fclose (fp);
     enviar_vetor(vetor,8735,"node17");
}

void receber_vetor(int V[]){

    int sockfd, newsockfd, porta, n, i;
    socklen_t clilen;
    int buffer[10240];
    struct sockaddr_in serv_addr, cli_addr;

    porta = 8735;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(porta);

    if (bind(sockfd, (struct sockaddr *) &serv_addr,
        sizeof(serv_addr)) < 0) 
        error("ERROR on binding");

    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, 
        (struct sockaddr *) &cli_addr, 
        &clilen);

    if (newsockfd < 0) 
        error("ERROR on accept");
    bzero(buffer,10240);
    n = read(newsockfd,buffer,10239);

    if (n < 0) error("ERROR reading from socket");

    for (i = 0; i < 250; i++){
        V[i] = buffer[i];
    }

    if (n < 0) error("ERROR writing to socket");
    close(newsockfd);
    close(sockfd);
}

// Função para ordenar o vetor
void insertionSort(int V[], int tam){
    int i, j, aux;
    for(i = 1; i < tam; i++){
        j = i;
        while((j != 0) && (V[j] < V[j - 1])) {
            aux = V[j];
            V[j] = V[j - 1];
            V[j - 1] = aux;
            j--;
        }
    }
}

void enviar_vetor(int V[],int porta,char* host)
{
    int sockfd, n, i;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    int buffer[10240];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(host);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(porta);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    bzero(buffer,10240);
        for(i=0;i<500;i++){
            buffer[i] = V[i];
        }
    n = write(sockfd,buffer,10240*sizeof(int));
    if (n < 0) 
         error("ERROR writing to socket");
    bzero(buffer,10240);
    close(sockfd);
}
